A = [0] * 3
print(A)
# [0, 0, 0]

for i in range(3):
    A[i] = [0] * 3

print(A)
# [[0, 0, 0], [0, 0, 0], [0, 0, 0]]

# 我们可以用下面这表达式！
S = [[0] * 3 for i in range(3)]

print(S)
# [[0, 0, 0], [0, 0, 0], [0, 0, 0]]
S[1][1] = 1
print(S)
# [[0, 0, 0], [0, 1, 0], [0, 0, 0]]

# 高级用法
# 生成一个偶数列表 (不建议使用汉字做变量名)
even = [i for i in range(31) if i % 2 == 0]
print(even)
# [0, 2, 4, 6, 8, 10, 12, 14, 16, 18, 20, 22, 24, 26, 28, 30]

even = [i + 1 for i in range(31) if i % 2 == 0]
# 顺序  最后    先执行               再执行
print(even)
# [1, 3, 5, 7, 9, 11, 13, 15, 17, 19, 21, 23, 25, 27, 29, 31]
good_words = ['Great', 'Brilliant', 'Excellent', 'Fantastic', 'Famous']

f_words = [w for w in good_words if w[0] == 'F']
# 意思是从good_words中迭代出字符串
# 因为字符串也是序列 所以就使用w[0]判断是否由F开头
print(f_words)
# ['Fantastic', 'Famous']

A = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
# 列表推导式还可以嵌套
# 将二维列表降维
B = [i
     for a in A
     for i in a if i % 2 == 1]  # 这个条件判断在每一个for后面都可以加
print(B)
# [1, 3, 5, 7, 9]

# 笛卡尔乘积
ep = [x + y
      for x in 'junzhao'
      for y in 'JUNZHAO']
print(ep)
# [
# 'jJ', 'jU', 'jN', 'jZ', 'jH', 'jA', 'jO',
# 'uJ', 'uU', 'uN', 'uZ', 'uH', 'uA', 'uO',
# 'nJ', 'nU', 'nN', 'nZ', 'nH', 'nA', 'nO',
# 'zJ', 'zU', 'zN', 'zZ', 'zH', 'zA', 'zO',
# 'hJ', 'hU', 'hN', 'hZ', 'hH', 'hA', 'hO',
# 'aJ', 'aU', 'aN', 'aZ', 'aH', 'aA', 'aO',
# 'oJ', 'oU', 'oN', 'oZ', 'oH', 'oA', 'oO'
# ]
"""
_ = []
for y in 'junzhao':
    for y in 'JUNZHAO':
        _.append(y + y)
ep2 = _
print(ep2) 结果跟上边是一样的
"""
#
ep3 = [[x, y]
       for x in range(7) if x % 2 == 0
       for y in range(7) if y % 2 == 1]
print(ep3)
# [
# [0, 1], [0, 3], [0, 5],
# [2, 1], [2, 3], [2, 5],
# [4, 1], [4, 3], [4, 5],
# [6, 1], [6, 3], [6, 5]
# ]

